const PAGE_SIZE = 3;
let data = [
  {
    id: 1,
    name: 'Leanne Graham',
    email: 'Sincere@april.biz',
    website: 'hildegard.org',
  },
  {
    id: 2,
    name: 'Ervin Howell',
    email: 'Shanna@melissa.tv',
    website: 'anastasia.net',
  },
  {
    id: 3,
    name: 'Clementine Bauch',
    email: 'Nathan@yesenia.net',
    website: 'ramiro.info',
  },
  {
    id: 4,
    name: 'Patricia Lebsack',
    email: 'Julianne.OConner@kory.org',
    website: 'kale.biz',
  },
  {
    id: 5,
    name: 'Chelsey Dietrich',
    email: 'Lucio_Hettinger@annie.ca',
    website: 'demarco.info',
  },
  {
    id: 6,
    name: 'Mrs. Dennis Schulist',
    email: 'Karley_Dach@jasper.info',
    website: 'ola.org',
  },
  {
    id: 7,
    name: 'Kurtis Weissnat',
    email: 'Telly.Hoeger@billy.biz',
    website: 'elvis.io',
  },
  {
    id: 8,
    name: 'Nicholas Runolfsdottir V',
    email: 'Sherwood@rosamond.me',
    website: 'jacynthe.com',
  },
  {
    id: 9,
    name: 'Glenna Reichert',
    email: 'Chaim_McDermott@dana.io',
    website: 'conrad.com',
  },
];

function uid(len?: number) {
  // eslint-disable-next-line no-param-reassign
  len = len || 7;
  return Math.random()
    .toString(35)
    .substr(2, len);
}

function getData(page: number) {
  const start = (page - 1) * PAGE_SIZE;
  return {
    content: data.slice(start, start + 3),
    empty: false,
    number: page - 1,
    numberOfElements: PAGE_SIZE,
    size: PAGE_SIZE,
    totalElements: data.length,
    totalPages: Math.round(data.length / PAGE_SIZE),
  };
}

export default {
  'GET /_api/userDashboard': (req: any, res: any) => {
    res.json(getData(parseInt(req.query.page, 10) || 1));
  },
  'DELETE /_api/userDashboard/:id': (req: any, res: any) => {
    data = data.filter(item => `${item.id}` !== `${req.params.id}`);
    res.end('ok');
  },
  'PATCH /_api/userDashboard/:id': (req: any, res: any) => {
    data.forEach(item => {
      if (`${item.id}` === `${req.params.id}`) {
        Object.assign(item, req.body);
      }
    });
    res.end('ok');
  },
  'POST /_api/userDashboard': (req: any, res:any) => {
    data.push({
      ...req.body,
      id: uid(),
    });
    res.end('ok');
  },
};
